package gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.IOException;

import view.MineArea;
import view.ShowRecord;

public class AppWindow extends JFrame implements MenuListener, ActionListener {
    JMenuBar bar;
    JMenu fileMenu;
    JMenu gradeOne, gradeTwo, gradeThree;//扫雷级别
    JMenuItem gradeOneList, gradeTwoList, gradeThreeList;//初，中，高级英雄榜
    MineArea mineArea = null;                //扫雷区域
    ShowRecord showHeroRecord = null;        //查看英雄榜

    public AppWindow() throws IOException {
        bar = new JMenuBar();
        fileMenu = new JMenu("扫雷游戏");
        gradeOne = new JMenu("初级");
        gradeTwo = new JMenu("中级");
        gradeThree = new JMenu("高级");
        gradeOneList = new JMenuItem("初级英雄榜");
        gradeTwoList = new JMenuItem("中级英雄榜");
        gradeThreeList = new JMenuItem("高级英雄榜");
        gradeOne.add(gradeOneList);
        gradeTwo.add(gradeTwoList);
        gradeThree.add(gradeThreeList);
        fileMenu.add(gradeOne);
        fileMenu.add(gradeTwo);
        fileMenu.add(gradeThree);
        bar.add(fileMenu);
        setJMenuBar(bar);
        gradeOne.addMenuListener(this);
        gradeTwo.addMenuListener(this);
        gradeThree.addMenuListener(this);
        gradeOneList.addActionListener(this);
        gradeTwoList.addActionListener(this);
        gradeThreeList.addActionListener(this);
        mineArea = new MineArea(9, 9, 10, gradeOne.getText());//创建初级扫雷区
        add(mineArea, BorderLayout.CENTER);
        showHeroRecord = new ShowRecord();
        setBounds(500, 300, 450, 500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();
    }

    public void menuSelected(MenuEvent e) {
        if (e.getSource() == gradeOne) {
            mineArea.initMineArea(9, 9, 10, gradeOne.getText());
            mineArea.initShow();
            validate();
        } else if (e.getSource() == gradeTwo) {
            mineArea.initMineArea(16, 16, 40, gradeTwo.getText());
            mineArea.initShow();
            validate();
        } else if (e.getSource() == gradeThree) {
            mineArea.initMineArea(22, 30, 99, gradeThree.getText());
            mineArea.initShow();
            validate();
        }
    }

    public void menuCanceled(MenuEvent e) {
    }

    public void menuDeselected(MenuEvent e) {
    }

    public static void main(String args[]) throws IOException {
        new AppWindow();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == gradeOneList) {
            showHeroRecord.setGrade(gradeOne.getText());
            try {
                showHeroRecord.showRecord();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } else if (e.getSource() == gradeTwoList) {
            showHeroRecord.setGrade(gradeTwo.getText());
            try {
                showHeroRecord.showRecord();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } else if (e.getSource() == gradeThreeList) {
            showHeroRecord.setGrade(gradeThree.getText());
            try {
                showHeroRecord.showRecord();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
